//DSP cluster即为一个包含24个DSP node的DSP簇，一个片上有4个簇
#include"DSP_node.h"
#include"instruction_module/instruction_mem.h"
#include<vector>
#include<unordered_map>
#include "Timer/Timer.h"

class DSP_cluster{
public:
    DSP_cluster() {
        instruction_mem = new Instruction_Mem();
        //TODO: GSM的size和start_offset均为测试时的临时值，后续需要根据实际情况进行调整
        GSM = new memory(1000000, 123);
        cluster_engine = new ClusterEngine();
        DSP_nodes = new std::vector<DSP_node*>(24, nullptr);
        // DSP_nodes->resize(24, nullptr); //每个簇包含24个DSP核
        thread_id_to_nodes_map = new std::unordered_map< int, std::vector<int> >();
        thread_id_to_status_map = new std::unordered_map<int, int>();
    }
    ~DSP_cluster() {
        delete instruction_mem;
        delete GSM;
        delete cluster_engine;
        delete DSP_nodes;
        delete thread_id_to_nodes_map;
        delete thread_id_to_status_map;
    }
    bool store_instruction(addr_size_t addr, instruction* instr, int para_nums,addr_size_t head_addr);
    instr_unit* get_instr(addr_size_t addr);
    bool store_function(std::string func_name, addr_size_t head_addr, addr_size_t end_addr);
    addr_unit* get_func(std::string func_name);
    //TODO:操作簇内DSP核的接口，例如开启线程等，group_create, group_masked_create
    int group_create(int num);
    int group_create(int num, char *func_name, int scalarArgs, int ptrArgs, uint64_t *argArray);
    int group_masked_create(unsigned int pmask, char *func_name, int scalarArgs, int ptrArgs, uint64_t *argArray);
    int group_exec(int thread_id, char *func_name, int scalarArgs, int ptrArgs, uint64_t *argArray);
    int group_get_status(int thread_id);
    void group_wait(int thread_id);
    int group_destroy(int thread_id);

private:
    std::unordered_map< int, std::vector<int> >* thread_id_to_nodes_map; 
    std::unordered_map<int, int>* thread_id_to_status_map; //记录线程组id和对应的状态
    //-1 -> 未创建，1 -> 运行中，0 -> 空闲中
    std::vector< DSP_node* >*  DSP_nodes;
    Instruction_Mem*    instruction_mem;
    memory* GSM;
    ClusterEngine*  cluster_engine;
};